<?php
/*
 * 鉴权解绑接口（API）   nmg_api_auth_unbind
 */
namespace app\api\controller;
use think\Request;
use think\Db;
use think\Controller;
use think\Session;
use changpay\ChangPay;
class Unbind extends Controller{
    function rsaSign($args) {
        $args=array_filter($args);//过滤掉空值
        ksort($args);
        $query  =   '';
        foreach($args as $k=>$v){
            if($k=='SignType'){
                continue;
            }
            if($query){
                $query  .=  '&'.$k.'='.$v;
            }else{
                $query  =  $k.'='.$v;
            }
        }
        //这地方不能用 http_build_query  否则会urlencode
        //$query=http_build_query($args);
        $path   =   "./rsa_private_key.pem";  //私钥地址
        $private_key= file_get_contents($path);
        $pkeyid = openssl_get_privatekey($private_key);
        openssl_sign($query, $sign, $pkeyid);
        openssl_free_key($pkeyid);
        $sign = base64_encode($sign);
        return $sign;
	}

    function rsaEncrypt($content){
    	$public_key_path   =   "./rsa_public_key.pem";  //公钥地址 
        $pubKey = file_get_contents($public_key_path);
        $res = openssl_get_publickey($pubKey);
        //把需要加密的内容，按128位拆开解密
        $result  = '';
        for($i = 0; $i < strlen($content)/128; $i++  ) {
            $data = substr($content, $i * 128, 128);
            openssl_public_encrypt ($data, $encrypt, $res);
            $result .= $encrypt;
        }
        $result = base64_encode($result);
        openssl_free_key($res);
        return $result;
    }
                

    function httpGet_a($order_url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $order_url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json = curl_exec($ch);
        curl_close($ch);
        return $json;
    }
    function unbind($bankNo,$userId){
        $postData = array();	
		// 基本参数
        $postData['Service'] = 'nmg_api_auth_unbind';
        $postData['Version'] = '1.0';
        $postData['PartnerId'] = '200002080023';//商户号
		$postData['InputCharset'] = 'UTF-8';
        $postData['TradeDate'] = date('Ymd').'';
        $postData['TradeTime'] = date('His').'';
		$postData['ReturnUrl'] = 'http://dev.chanpay.com/receive.php';// 前台跳转url
		$postData['Memo'] = '备注';

		// 4.4.2.4	鉴权解绑接口（API） 业务参数
        $postData['TrxId'] = time(); //外部流水号
		$postData['MerchantNo'] =   '200002080023'; //商户编号
		$postData['MerUserId'] =   $userId; //用户标识
	    $postData['UnbindType'] =   '1'; //解绑模式，0为物理删除，1为逻辑删除
	    $postData['CardBegin'] = substr($bankNo,0,6); //卡号前6位
		$postData['CardEnd'] =  substr($bankNo,-4);//卡号后4位
		$postData['Extension'] = '';//扩展字段

        $postData['Sign'] = $this->rsaSign($postData);
	    $postData['SignType'] = 'RSA'; //签名类型
		
		$query = http_build_query($postData);
		$url = 'https://pay.chanpay.com/mag-unify/gateway/receiveOrder.do?'. $query; //该url为生产环境url
		$jsonstr = $this->httpGet_a($url);
		$json = json_decode($jsonstr);
		echo $jsonstr; 
    }

}
?>    